home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / bestl232 / !best001.c next >
C/C++ Source or Header  |  1994-05-13  |  28KB  |  720 lines

  1. /*==========================================================================
  2.  *
  3.  *  !BEST001.C                                       Tuesday, April 12, 1994
  4.  *
  5.  *  con_ str_ fil_ counter counter_reg dos_shell
  6.  *  supplementary source file 1 for The BESTLibrary
  7.  *
  8.  *  Authored independently by George Vanous
  9.  *
  10.  *==========================================================================*/
  11.  
  12.  
  13. /* ------------------------------------------------------------------------ */
  14. /* ----------------------------  INCLUDE FILES  --------------------------- */
  15.  
  16. #include <dir.h>
  17. #include <alloc.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <process.h>
  21. #include "!bestlib.h"
  22.  
  23. /* ------------------------------------------------------------------------ */
  24. /* ------------------------------  MESSAGES  ------------------------------ */
  25.  
  26. #define MSG01 "\nThis is your first time using %s\nI, %s, hope you enjoy this program\n\n"
  27. #define MSG02 "\n%s has been used %d time"
  28. #define MSG03 "\nThere is %ld bytes of free RAM now available to %s\n"
  29. #define MSG04 "\nThis copy of %s is registered to %s\n"
  30. #define MSG05 "\nThis is the master copy of %s\nIt is registered to its author, %s\n"
  31. #define ERR01 "\n!!! ERROR !!!  I cannot find the data file %s\n"
  32.  
  33. /* ------------------------------------------------------------------------ */
  34.  
  35.  
  36. /*----------------------------------------------------------------------------
  37.  * Shell to DOS.
  38.  *
  39.  * RETURNS:
  40.  * = pointer to an error message
  41.  * = NULL if no error message
  42.  */
  43. char *dos_shell(void)
  44. {
  45.   int error;
  46.   char *commandpath, *textstring;
  47.   struct ffblk ffblk;
  48.  
  49.   if ((commandpath = getenv("COMSPEC")) == NULL &&
  50.      (commandpath = searchpath("COMMAND.COM")) == NULL) {
  51.     if ((error = findfirst(commandpath = "COMMAND.COM", &ffblk, 0)) == NULL &&
  52.         (error = findfirst(commandpath = "C:\COMMAND.COM", &ffblk, 0)) == NULL &&
  53.         (error = findfirst(commandpath = "C:\DOS\COMMAND.COM", &ffblk, 0)) == NULL &&
  54.         (error = findfirst(commandpath = "C:\BIN\COMMAND.COM", &ffblk, 0)) == NULL) {
  55.       textstring = "Cannot find file COMMAND.COM -- initialize COMSPEC=[path]COMMAND.COM";
  56.       return( textstring );
  57.     }
  58.   }
  59.   video_set(TEXT);
  60.   error = spawnl(P_WAIT, commandpath, commandpath, NULL);
  61.  
  62.   if (error == -1) {
  63.     switch(errno) {
  64.     case EINVAL : textstring = "Bad argument passed to COMMAND.COM"; break;
  65.     case E2BIG  : textstring = "Too many arguments passed to COMMAND.COM"; break;
  66.     case ENOEXEC: textstring = "Internal error -- exitting strongly suggested"; break;
  67.     case ENOMEM : textstring = "Out of memory -- cannot shell to DOS"; break;
  68.     case ENOENT : textstring = "Cannot find file COMMAND.COM -- initialize COMSPEC=[path]COMMAND.COM";
  69.     }
  70.   }
  71.   else if (!error) textstring = NULL;
  72.   return( textstring );
  73. }
  74.  
  75. /*----------------------------------------------------------------------------
  76.  * Read, print, and increment the number of program executions.
  77.  * Print a comment, depending on the current execution number.
  78.  * Print the amount of available memory.
  79.  *
  80.  * "filename" - name of the text data file to read the number of program
  81.  *              executions from (this number must be the first byte)
  82.  *            - writes the incremented number of executions back to the file
  83.  * "title"    - name of the program
  84.  * "author"   - name of the author of the program
  85.  * "msgs"     - one comment will be randomly chosen from this array of
  86.  *              messages
  87.  * "msg_num"  - number of messages present
  88.  *
  89.  * RETURNS:
  90.  * = file handle of the opened data file
  91.  */
  92. FILE *fil_count(char *filename, char *title, char *author, char *msgs[], word msg_num)
  93. {
  94.   word counter;                        // usage counter
  95.   FILE *f;                             // file handle
  96.  
  97.   DASHES;                              // print one line of dashes
  98.   if ((f = fopen(filename, "r+b")) == NULL) {
  99.     fprintf(stderr, ERR01, filename);  // file could not be opened
  100.     exit(1);                           // abort with ERRORLEVEL 1
  101.   }
  102.  
  103.   /* the first byte of the file is the usage counter -- get it */
  104.   fread(&counter, sizeof(counter), 1, f);
  105.  
  106.   /* increment the counter and print the correct message */
  107.   if (counter++ == 0)
  108.     printf(MSG01, title, author);      // print "first time used" message
  109.   else {
  110.     printf(MSG02, title, counter);     // print number of program uses
  111.     if (counter != 1)                  // if uses do not equal 1 then
  112.       printf("s");                     //   pluralise
  113.   }
  114.   DOUBLESPACE;
  115.  
  116.   /* write the incremented counter back to the file */
  117.   rewind(f);                           // go to beginning of file
  118.   fwrite(&counter, sizeof(counter), 1, f);
  119.  
  120.   // the "fseek" is REQUIRED for the next "fread" to work???
  121.   fseek(f, 0L, SEEK_CUR);
  122.  
  123.   printf(msgs[random(msg_num)], title);
  124.   printf(MSG03, coreleft(), title);    // print amount of available memory
  125.   return( f );                         // return file handle
  126. }
  127.  
  128. /*----------------------------------------------------------------------------
  129.  * Read, print, and increment the number of program executions.
  130.  * Print to whom this program is registered.
  131.  * Print the amount of available memory.
  132.  *
  133.  * "filename"  - name of the text data file to read the number of program
  134.  *               executions from (this number must be the first byte)
  135.  *             - writes the incremented number of executions back to the file
  136.  * "title"     - name of the program
  137.  * "author"    - name of the author of the program
  138.  * "registree" - name of the person to whom this program is registered
  139.  *
  140.  * RETURNS:
  141.  * = file handle of the opened data file
  142.  */
  143. FILE *fil_count_reg(char *filename, char *title, char *author, char *registree)
  144. {
  145.   word counter;                        // usage counter
  146.   FILE *f;                             // file handle
  147.  
  148.   DASHES;                              // print one line of dashes
  149.   if ((f = fopen(filename, "r+b")) == NULL) {
  150.     fprintf(stderr, ERR01, filename);  // file could not be opened
  151.     exit(1);                           // abort with ERRORLEVEL 1
  152.   }
  153.  
  154.   /* the first byte of the file is the usage counter -- get it */
  155.   fread(&counter, sizeof(counter), 1, f);
  156.  
  157.   /* increment the counter and print the correct message */
  158.   if (counter++ == 0)
  159.     printf(MSG01, title, author);      // print "first time used" message
  160.   else {
  161.     printf(MSG02, title, counter);     // print number of program uses
  162.     if (counter != 1)                  // if uses do not equal 1 then
  163.       printf("s");                     //   pluralise
  164.   }
  165.   DOUBLESPACE;
  166.  
  167.   /* write the incremented counter back to the file */
  168.   rewind(f);                           // go to beginning of file
  169.   fwrite(&counter, sizeof(counter), 1, f);
  170.  
  171.   // the "fseek" is REQUIRED for the next "fread" to work???
  172.   fseek(f, 0L, SEEK_CUR);
  173.  
  174.   if (!str_cmp(author, registree))     // if this is registered to author then
  175.     printf(MSG05, title, author);      //   print master copy message
  176.   else                                 // else
  177.     printf(MSG04, title, registree);   //   print to whom this is registered
  178.  
  179.   printf(MSG03, coreleft(), title);    // print amount of available memory
  180.   return( f );                         // return file handle
  181. }
  182.  
  183. /*----------------------------------------------------------------------------
  184.  * Return the length of a file, in bytes.
  185.  *
  186.  * "f" - file handle of file to return length of
  187.  *
  188.  * RETURNS:
  189.  * = length of file in bytes
  190.  */
  191. long fil_len(FILE *f)
  192. {
  193.   long curpos, length;
  194.  
  195.   curpos = ftell(f);                   // save current position
  196.   fseek(f, 0L, SEEK_END);              // go to EOF
  197.   length = ftell(f);                   // get EOF position relative to start
  198.   fseek(f, curpos, SEEK_SET);          // go back to original position
  199.   return( length );                    // return length of file, in bytes
  200. }
  201.  
  202. /*----------------------------------------------------------------------------
  203.  * Read the next string from the current file position.
  204.  *
  205.  * "size" - initial size of text input buffer
  206.  * "f"    - file handle of file to read from
  207.  *
  208.  * RETURNS:
  209.  * = pointer to next string (file pointer is set to byte just beyond word)
  210.  * = NULL if no string was found (file pointer is set to EOF)
  211.  */
  212. char *fil_next_str(word *size, FILE *f)
  213. {
  214.   char ch,                             // temporary character-holder
  215.       *text;                           // text input buffer
  216.   word index = 0,                      // index into text input buffer
  217.        size_org;                       // original initial size of buffer
  218.  
  219.   text = (char *) malloc(size_org = *size);
  220.  
  221.   /* ignore initial whitespaces */
  222.   for (ch = getc(f); !is_letternum(ch) && ch != QUOTE; ch = getc(f)) {
  223.     if (ch == EOF) {                   // if we hit end of file
  224.       free(text);                      //  no match was found (free up memory)
  225.       return( NULL );                  //  return NULL
  226.     }
  227.   }
  228.  
  229.   /* read in next string */
  230.   if (ch == QUOTE) {
  231.     do {
  232.       if (ch == EOF) {                 // if we hit end of file
  233.         free(text);                    //  no match was found (free up memory)
  234.         return( NULL );                //  return NULL
  235.       }
  236.       while (index+2 >= *size)         // allocate more memory if required
  237.         text = (char *) realloc(text, *size += size_org);
  238.       text[index++] = ch;              // store next valid character
  239.       ch = getc(f);                    // get next character
  240.     } while (ch != QUOTE               // loop until next character is a '"'
  241.           && ch != CR && ch != LF);    //  or until it is a CR or LF
  242.  
  243.     if (ch == QUOTE)                   // if the last character was a '"'
  244.       text[index++] = ch;              //  store it
  245.   }
  246.   else {
  247.     for ( ; is_letternum(ch); ch = getc(f) ) {
  248.       if (index+1 >= *size)            // allocate more memory if required
  249.         text = (char *) realloc(text, *size += size_org);
  250.       text[index++] = ch;              // store next valid character
  251.     }
  252.   }
  253.  
  254.   text[index] = NULL;                  // end string with NULL-terminator
  255.   return( text );                      // return next string
  256. }
  257.  
  258. /*----------------------------------------------------------------------------
  259.  * Read the next word from the current file position.
  260.  *
  261.  * "size" - initial size of text input buffer
  262.  * "f"    - file handle of file to read from
  263.  *
  264.  * RETURNS:
  265.  * = pointer to next word (file pointer is set to byte just beyond word)
  266.  * = NULL if no word was found (file pointer is set to EOF)
  267.  */
  268. char *fil_next_word(word *size, FILE *f)
  269. {
  270.   char ch,                             // temporary character-holder
  271.       *text;                           // text input buffer
  272.   word index = 0,                      // index into text input buffer
  273.        size_org;                       // original initial size of buffer
  274.  
  275.   text = (char *) malloc(size_org = *size);
  276.  
  277.   /* ignore initial whitespaces */
  278.   for (ch = getc(f); !is_letter(ch); ch = getc(f) ) {
  279.     if (ch == EOF) {                   // if we hit end of file
  280.       free(text);                      //  no match was found (free up memory)
  281.       return( NULL );                  //  return NULL
  282.     }
  283.   }
  284.  
  285.   /* read in next word */
  286.   for ( ; is_letter(ch); ch = getc(f)) {
  287.     if (index+1 >= *size)              // allocate more memory if required
  288.       text = (char *) realloc(text, *size += size_org);
  289.     text[index++] = ch;                // store valid alphabet character
  290.   }
  291.  
  292.   text[index] = NULL;                  // end string with NULL-terminator
  293.   return( text );                      // return next word
  294. }
  295.  
  296. /*----------------------------------------------------------------------------
  297.  * Read from the current file position upto the first occurrence of a string.
  298.  *
  299.  * "str" - string to read up to
  300.  * "size" - initial size of text input buffer
  301.  * "f"   - file handle of file to read from
  302.  *
  303.  * RETURNS:
  304.  * = pointer to contents of file read in (file pointer is set to byte just
  305.  *   beyond match
  306.  * = NULL if no match was found (file pointer is set to EOF)
  307.  */
  308. char *fil_read_to(char *str, word *size, FILE *f)
  309. {
  310.   char ch,                             // temporary character holder
  311.       *text;                           // text input buffer
  312.   int  index_str;                      // index into string-to-read-to
  313.    //  must be integer because: fseek(f, -index_str, SEEK_CUR); (-ve seek)
  314.   word index = 0,                      // index into text input buffer
  315.        size_org;                       // original initial size of buffer
  316.  
  317.   text = (char *) malloc(size_org = *size);
  318.  while (TRUE)
  319.  {
  320.   while ( (text[index++] = getc(f)) != str[0] ) {
  321.     if (text[index-1] == EOF) {        // if we hit end of file
  322.       free(text);                      //  no match was found (free up memory)
  323.       return( NULL );                  //  return NULL
  324.     }
  325.     if (index+1 >= *size)              // allocate more memory if required
  326.       text = (char *) realloc(text, *size += size_org);
  327.   }
  328.  
  329.   if (str_len(str) == 1) {
  330.     text[1] = NULL;                    // end string with NULL-terminator
  331.     return( text );                    // return pointer to text input holder
  332.   }
  333.   else {
  334.     for (index_str = 0; (ch = getc(f)) == str[++index_str]; );
  335.     if (index_str == str_len(str)) {   // if we found the string
  336.       fseek(f, -1, SEEK_CUR);          //  rewind to byte just after match
  337.       if (index+index_str > *size)     //  allocate more memory if required
  338.         text = (char *) realloc(text, *size = index_str+index);
  339.       str_copy(text + index, str+1);
  340.       return( text );                  //  return pointer to text input holder
  341.     }
  342.     else if (ch == EOF) {              // if we hit end of file
  343.       free(text);                      //  no match was found (free up memory)
  344.       return( NULL );                  //  return NULL
  345.     }
  346.   }
  347.  
  348.   fseek(f, -index_str, SEEK_CUR);      // false alarm -- rewind
  349.  }
  350. }
  351.  
  352. /*----------------------------------------------------------------------------
  353.  * Read from the current file position upto the first occurrence of a string.
  354.  * All comments (beginning with a ';'), blank lines, and extra spaces are
  355.  *   stripped to conserve memory.
  356.  *
  357.  * "str" - string to read up to
  358.  * "size" - initial size of text input buffer
  359.  * "f"   - file handle of file to read from
  360.  *
  361.  * RETURNS:
  362.  * = pointer to contents of file read in (file pointer is set to byte just
  363.  *   beyond match
  364.  * = NULL if no match was found (file pointer is set to EOF)
  365.  */
  366. char *fil_read_to_strip(char *str, word *size, FILE *f)
  367. {
  368.   char ch,                             // temporary character holder
  369.       *text;                           // text input buffer
  370.   int  index_str,                      // index into string-to-read-to
  371.        quotes = FALSE;                 // [inside a quote?] flag
  372.   word index = 0,                      // generic buffer character index
  373.        size_org,                       // original initial size of buffer
  374.        spaces = 1;                     // count of ' ' (space) characters
  375.  
  376.   text = (char *) malloc(size_org = *size);
  377.  while (TRUE)
  378.  {
  379.   do {
  380.     ch = getc(f);                      // get next character
  381.     while (TRUE)
  382.     {
  383.       if (ch != ' ') spaces = 0;       // reset space counter
  384.  
  385.       if (ch == '[') {
  386.         if (!quotes)          quotes = 2;
  387.         break;
  388.       }
  389.       if (ch == ']') {
  390.         if (quotes == 2)      quotes = FALSE;
  391.         break;
  392.       }
  393.       if (ch == '"') {
  394.         if (!quotes)          quotes = 1;
  395.         else if (quotes == 1) quotes = FALSE;
  396.         break;
  397.       }
  398.  
  399.       if (ch == CR) {
  400.         // clear out all following CR/LF pairs
  401.         for (ch = getc(f), ch = getc(f); ch == CR || ch == LF; ch = getc(f));
  402.         quotes = FALSE, spaces = 1;
  403.         if (index) {                   // if not first character
  404.           text[index++] = LF;          //  store one EOL character
  405.           if (index+1 >= *size)        //  allocate more memory if required
  406.             text = (char *) realloc(text, *size += size_org);
  407.         }
  408.         continue;                      // check out new character
  409.       }
  410.  
  411.       if (!quotes) {
  412.         if (ch == ';') {               // remove all comments
  413.           if (text[index-1] == ' ')
  414.             index--;
  415.           while ((ch = getc(f)) != CR && ch != EOF);
  416.           continue;                    // check out new character
  417.         }
  418.         if (ch == '=') {               // ignore empty spaces surrounding '='
  419.           if (text[index-1] == ' ')
  420.             index--;
  421.           spaces = 1;                  // if it is a space, will remove it
  422.           break;                       // go store '='
  423.         }
  424.         if (ch == ' ' && ++spaces > 1) { // ignore 2+ blank spaces
  425.           while ((ch = getc(f)) == ' ');
  426.           continue;                    // check out new character
  427.         }
  428.       }
  429.  
  430.       if (ch == EOF) {                 // if we hit end of file
  431.         free(text);                    //  no match was found (free up memory)
  432.         return( NULL );                //  return NULL
  433.       }
  434.       break;                           // character is valid, so go store it
  435.     }
  436.     text[index++] = ch;                // store valid character
  437.     if (index+1 >= *size)              // allocate more memory if required
  438.       text = (char *) realloc(text, *size += size_org);
  439.   } while (ch != str[0]);
  440.  
  441.   if (str_len(str) == 1) {
  442.     text[1] = NULL;                    // end string with NULL-terminator
  443.     return( text );                    // return pointer to text input holder
  444.   }
  445.   else {
  446.     for (index_str = 0; (ch = getc(f)) == str[++index_str]; );
  447.     if (index_str == str_len(str)) {   // if we found the string
  448.       fseek(f, -1, SEEK_CUR);          //  rewind to byte just after match
  449.       if (index+index_str > *size)     //  allocate more memory if required
  450.         text = (char *) realloc(text, *size = index_str+index);
  451.       str_copy(text + index, str+1);
  452.       return( text );                  //  return pointer to text input holder
  453.     }
  454.     else if (ch == EOF) {              // if we hit end of file
  455.       free(text);                      //  no match was found (free up memory)
  456.       return( NULL );                  //  return NULL
  457.     }
  458.   }
  459.  
  460.   fseek(f, -index_str, SEEK_CUR);      // false alarm -- rewind
  461.  }
  462. }
  463.  
  464. /*----------------------------------------------------------------------------
  465.  * Search a file for a string and set the file pointer just past it.
  466.  *
  467.  * "str" - string to skip past
  468.  * "f"   - file handle of file to read from
  469.  *
  470.  * RETURNS:
  471.  * The file pointer is set to one byte beyond the match or
  472.  *   to the end-of-file if no match was found.
  473.  * = number of bytes scanned upto (and including) last byte of match
  474.  * = FALSE if no match was found
  475.  */
  476. word fil_skip_past(char *str, FILE *f)
  477. {
  478.   int  index;                          // input string character index
  479.   char ch;                             // temporary character holder
  480.   word offset = 0;                     // file character offset
  481.  
  482.  while (TRUE)
  483.  {
  484.   for (offset++; (ch = getc(f)) != str[0]; offset++)
  485.     if (ch == EOF) return( FALSE );
  486.  
  487.   if (str_len(str) == 1)
  488.     return( offset );
  489.   else {
  490.     for (index = 0, offset++; (ch = getc(f)) == str[++index]; offset++);
  491.     if (index == str_len(str)) {       // if we found the string
  492.       fseek(f, -1, SEEK_CUR);          //   rewind to byte just after match
  493.       return( offset-1 );              //   return ending offset of find
  494.     }
  495.     else if (ch == EOF)                // if we hit end of file
  496.       return( FALSE );                 //   return FALSE
  497.   }
  498.  
  499.   fseek(f, -index, SEEK_CUR);          // false alarm -- rewind
  500.   offset -= index;                     // false alarm -- rewind
  501.  }
  502. }
  503.  
  504. /*----------------------------------------------------------------------------
  505.  * Search a file for a string and set the file pointer to it.
  506.  *
  507.  * "str" - string to set file pointer to
  508.  * "f"   - file handle of file to read from
  509.  *
  510.  * RETURNS:
  511.  * The file pointer is set to the first byte of the first match
  512.  *   the end-of-file if no match was found.
  513.  * = number of bytes scanned upto (and including) first byte of match
  514.  *   file pointer set to the first byte of match
  515.  * = FALSE if no match was found
  516.  *   file pointer set to end-of-file
  517.  */
  518. word fil_skip_to(char *str, FILE *f)
  519. {
  520.   int  index;                          // input string character index
  521.   char ch;                             // temporary character holder
  522.   word offset = 0;                     // file character offset
  523.  
  524.  while (TRUE)
  525.  {
  526.   for (offset++; (ch = getc(f)) != str[0]; offset++)
  527.     if (ch == EOF) return( FALSE );
  528.  
  529.   if (str_len(str) == 1) {
  530.     fseek(f, -1, SEEK_CUR);            // rewind to beginning of match
  531.     return( offset );
  532.   }
  533.   else {
  534.     for (index = 0, offset++; (ch = getc(f)) == str[++index]; offset++);
  535.     if (index == str_len(str)) {       // if we found the string
  536.       fseek(f, -(index+1), SEEK_CUR);  //   rewind to first byte of match
  537.       return( offset-index );          //   return beginning offset of find
  538.     }
  539.     else if (ch == EOF)                // if we hit end of file
  540.       return( FALSE );                 //   return FALSE
  541.   }
  542.  
  543.   fseek(f, -index, SEEK_CUR);          // false alarm -- rewind
  544.   offset -= index;                     // false alarm -- rewind
  545.  }
  546. }
  547.  
  548. /*----------------------------------------------------------------------------
  549.  * Return the next string from a string in memory.
  550.  *
  551.  * "text" - string to get the next string from
  552.  *
  553.  * RETURNS:
  554.  * = pointer to next string
  555.  */
  556. char *str_next_str(char *text)
  557. {
  558.   char *str;                           // string to return
  559.   word begin,                          // offset to beginning of next word
  560.        index;                          // index into string to read from
  561.  
  562.   /* ignore initial whitespaces */
  563.   for (index = 0; !is_letternum(text[index]) && text[index] != QUOTE; index++)
  564.     if (!text[index]) return( NULL );  // if we hit end of string, return NULL
  565.  
  566.   /* get length of next word */
  567.   begin = index;                       // save offset to beginning of nextword
  568.   if (text[index] == QUOTE) {
  569.     while (TRUE) {
  570.       if (!text[index])                // if we hit end of string
  571.         return( NULL );                //  return NULL
  572.       if (text[++index] == QUOTE) {    // if next character is a '"'
  573.         index++;                       //  make sure the end quote is stored
  574.         break;                         //  break out of loop
  575.       }
  576.       if (text[index] == CR ||
  577.           text[index] == LF)           // if we hit end of line
  578.         break;                         //  break out of loop
  579.     }
  580.   }
  581.   else
  582.     while (is_letternum(text[++index]));
  583.  
  584.   /* allocate memory and make copy of the word */
  585.   str = (char *) malloc(index-begin + 1);
  586.   mem_copy(str, text+begin, index-begin);
  587.   str[index-begin] = NULL;             // end string with NULL-terminator
  588.  
  589.   return( str );                       // return next string
  590. }
  591.  
  592. /*----------------------------------------------------------------------------
  593.  * Return the next word from a string in memory.
  594.  *
  595.  * "text" - string to get the next word from
  596.  *
  597.  * RETURNS:
  598.  * = pointer to next word
  599.  */
  600. char *str_next_word(char *text)
  601. {
  602.   char *str;                           // str input buffer
  603.   word  begin,                         // offset to beginning of next word
  604.         index;                         // index into string to read from
  605.  
  606.   /* ignore initial whitespaces */
  607.   for (index = 0; !is_letter(text[index]); index++);
  608.  
  609.   /* get length of next word */
  610.   for (begin = index; is_letter(text[++index]); );
  611.  
  612.   /* allocate memory and make a copy of the word */
  613.   str = (char *) malloc(index-begin + 1);
  614.   mem_copy(str, text+begin, index-begin);
  615.   str[index-begin] = NULL;             // end word with NULL-terminator
  616.  
  617.   return( str );                       // return next word
  618. }
  619.  
  620. /*----------------------------------------------------------------------------
  621.  * Convert the boolean FALSE or TRUE into the string "FALSE" or "TRUE",
  622.  * respectively.
  623.  *
  624.  * "bool" - boolean to convert
  625.  *
  626.  * RETURNS:
  627.  * = "TRUE"  if "bool" = TRUE
  628.  * = "FALSE" if "bool" = FALSE
  629.  */
  630. char *con_bool_to_str(boolean bool)
  631. {
  632.   if (!bool)                           // is boolean = FALSE
  633.     return( "FALSE" );                 //  yes, so return "FALSE"
  634.   return( "TRUE" );                    // else return "TRUE"
  635. }
  636.  
  637. /*----------------------------------------------------------------------------
  638.  * Convert the numeric representation of a color to its string equivalent.
  639.  *
  640.  * "color" - color to convert
  641.  *
  642.  * RETURNS:
  643.  * = string representation of "color"
  644.  * = NULL if "color" is not a valid color
  645.  */
  646. char *con_color_to_str(byte color)
  647. {
  648.   if (color == BLACK       ) return( "BLACK"        );
  649.   if (color == BLUE        ) return( "BLUE"         );
  650.   if (color == GREEN       ) return( "GREEN"        );
  651.   if (color == CYAN        ) return( "CYAN"         );
  652.   if (color == RED         ) return( "RED"          );
  653.   if (color == MAGENTA     ) return( "MAGENTA"      );
  654.   if (color == BROWN       ) return( "BROWN"        );
  655.   if (color == LIGHTGREY   ) return( "LIGHTGREY"    );
  656.   if (color == DARKGREY    ) return( "DARKGREY"     );
  657.   if (color == LIGHTBLUE   ) return( "LIGHTBLUE"    );
  658.   if (color == LIGHTGREEN  ) return( "LIGHTGREEN"   );
  659.   if (color == LIGHTCYAN   ) return( "LIGHTCYAN"    );
  660.   if (color == LIGHTRED    ) return( "LIGHTRED"     );
  661.   if (color == LIGHTMAGENTA) return( "LIGHTMAGENTA" );
  662.   if (color == YELLOW      ) return( "YELLOW"       );
  663.   if (color == WHITE       ) return( "WHITE"        );
  664.   return( NULL );                      // no color matched -- return NULL
  665. }
  666.  
  667. /*----------------------------------------------------------------------------
  668.  * Convert the string representation of a boolean value into boolean FALSE or TRUE,
  669.  * respectively.
  670.  *
  671.  * "str" - word to convert
  672.  *
  673.  * RETURNS:
  674.  * = TRUE  if "str" = "TRUE"
  675.  * = FALSE if "str" = "FALSE"
  676.  * = -1    if "str" is not a valid boolean word
  677.  */
  678. shortint con_str_to_bool(char *str)
  679. {
  680.   str_case_up(str);                    // make sure we are in uppercase
  681.   if (!str_cmp(str, "FALSE"))          // is string = "FALSE"
  682.     return( FALSE );                   //  yes, so return FALSE
  683.   if (str_cmp(str, "TRUE"))            // is string = "TRUE"
  684.     return( -1 );                      //  no, so return -1
  685.   return( TRUE );                      // else return TRUE
  686. }
  687.  
  688. /*----------------------------------------------------------------------------
  689.  * Convert the name of a color into its numeric equivalent.
  690.  *
  691.  * "str" - color to convert
  692.  *
  693.  * RETURNS:
  694.  * = color value
  695.  * = -1 if "str" is not a valid color
  696.  */
  697. shortint con_str_to_color(char *str)
  698. {
  699.   str_case_up(str);                    // make sure we are in uppercase
  700.   if (!str_cmp(str, "BLACK")       ) return( BLACK        );
  701.   if (!str_cmp(str, "BLUE")        ) return( BLUE         );
  702.   if (!str_cmp(str, "GREEN")       ) return( GREEN        );
  703.   if (!str_cmp(str, "CYAN")        ) return( CYAN         );
  704.   if (!str_cmp(str, "RED")         ) return( RED          );
  705.   if (!str_cmp(str, "MAGENTA")     ) return( MAGENTA      );
  706.   if (!str_cmp(str, "BROWN")       ) return( BROWN        );
  707.   if (!str_cmp(str, "LIGHTGREY")   ) return( LIGHTGREY    );
  708.   if (!str_cmp(str, "DARKGREY")    ) return( DARKGREY     );
  709.   if (!str_cmp(str, "LIGHTBLUE")   ) return( LIGHTBLUE    );
  710.   if (!str_cmp(str, "LIGHTGREEN")  ) return( LIGHTGREEN   );
  711.   if (!str_cmp(str, "LIGHTCYAN")   ) return( LIGHTCYAN    );
  712.   if (!str_cmp(str, "LIGHTRED")    ) return( LIGHTRED     );
  713.   if (!str_cmp(str, "LIGHTMAGENTA")) return( LIGHTMAGENTA );
  714.   if (!str_cmp(str, "YELLOW")      ) return( YELLOW       );
  715.   if (!str_cmp(str, "WHITE")       ) return( WHITE        );
  716.   return( -1 );                        // no color matched -- return -1
  717. }
  718.  
  719. /*==============================  END-OF-FILE  =============================*/
  720.